package com.sms.note;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sms.DB.ConnectDB;
import com.sms.DB.OperateDB;
import com.sms.project.Task;

public class Note {
	private int infoid;
	private String fromid;
	private String fromname;
	private String notetime;
	private String content;
	private String title;
	private int readnum;
	private String isappend;
	private List<Map> appendFile = null;//公告附件
	public void init(Map map){
		//if(map == null)System.out.println("null");
		//System.out.println((Integer)map.get("p_reportid"));
		this.setInfoid((Integer)map.get("infoid"));
		this.setFromid((String)map.get("fromid"));
		this.setFromname((String)map.get("name"));
		this.setNotetime(map.get("notetime").toString());
		this.setTitle((String)map.get("title"));
		this.setReadnum((Integer)map.get("readnum"));
		this.setContent((String)map.get("content"));
		this.setIsappend((String)map.get("isappend"));		
		if(isappend.equals("1")) {
			this.setAppendFile((Integer)map.get("infoid"));
		}
		
		
	}	
	
	//返回指定id的公告详情
		public static List<Map> getdetailinfo(int infoid){
			String sql1="select * from publicinfo,allpeople where fromid=mno and infoid='"+infoid+"'";
			List<Map> list1 = new ArrayList<Map>();
			list1 = OperateDB.executeQuery(sql1);
			Map map=list1.get(0);
			
			if(map.get("isappend").equals('1')){
			String sql="select * from publicinfo ,allpeople,appendinfo where fromid=mno and append_type='公告' and infoid=fileid and infoid='"+infoid+"'";
			
			List<Map> list = new ArrayList<Map>();
			list = OperateDB.executeQuery(sql);
				list1=list;
			}
			return list1;	
		}
	//更新公告内容
		public static boolean updatenote(int infoid,String title,String content){
			boolean isSuccess=true;
			String sql="update publicinfo set title='"+title+"',content='"+content+"'where infoid ="+infoid;
			System.out.println(sql);
			int n = OperateDB.update(sql);
				if (n == 0) {// 删除附件失败
					isSuccess = false; 
				}
			 return isSuccess;	
			
		}
	//更新浏览次数
	
	public static void countupdate(int num,int infoid){
		String sql = "update publicinfo set readnum="+num+" where infoid = " + infoid;	
		OperateDB.update(sql);		
		
	}
	
	
	
	//获取指定文件的附件并设置
			public void setAppendFile(int fileID) {
				String sql = "select * from appendinfo where (append_type = '公告' )" +
						"and fileid = " + fileID;
				
				List<Map> list = new ArrayList<Map>();
				list = OperateDB.executeQuery(sql);
				if(list.size() > 0) {//有附件时才初始化appendPath对象
					appendFile = new ArrayList<Map>();
					for(int i=0; i<list.size(); i++) {
						Map map = list.get(i);
						String type=(String)map.get("append_type");
						String fileName = (String)map.get("filename");
						String saveName = (String)map.get("savename");
						Map<String, String> map2 = new HashMap<String, String>();
						map2.put("type", type);
						map2.put("fileName", fileName);
						map2.put("saveName", saveName);
						appendFile.add(map2);
					}
				}
			}
	
	//查询全部公告发布情况
	public static List<Map> query(){
		String sql="select * from publicinfo,allpeople where fromid=mno order by notetime desc ";
		
		
		
		return OperateDB.executeQuery(sql);
		
	}	
	
	//查询自己公告发布情况
			public static List<Map> querysigle(int infoid){
				String sql="select * from publicinfo,allpeople where fromid=mno and infoid='"+infoid+"'";			
				return OperateDB.executeQuery(sql);
				
			}	
	
	
	
	//查询自己公告发布情况
		public static List<Map> queryself(String fromid){
			String sql="select * from publicinfo,allpeople where fromid=mno and fromid='"+fromid+"' order by notetime desc ";			
			return OperateDB.executeQuery(sql);
			
		}	
	
	
	//发布公告后插入数据库并返回 boolean值
	public static boolean inserData(String fromID,String title ,String content,  List<Map> appendFile) {
		String sql = "";

		boolean isSuccess = false;
		
		Connection conn = ConnectDB.getConnection();
		boolean autoCommit;
		try {
			autoCommit = conn.getAutoCommit();
			conn.setAutoCommit(false);// 取消自动提交

			// 向publicinfo表插入数据
			sql = "insert into publicinfo values(null, ?, now(),?,  ?,  '0',0)";
			List<Object> tlist = new ArrayList<Object>();
			tlist.add(fromID);
			tlist.add(title);		
			tlist.add(content);								
			OperateDB.setList(tlist);
			int tn = OperateDB.getGeneratedKey(sql);

			// System.out.println(tn);
			if (tn == 0) { // 插入publicinfo不成功
				isSuccess = false;
			} else {
				if (appendFile == null || appendFile.size() < 1) {// 没有添加附件时
					//System.out.println("没有附件");
					isSuccess = true;
				} else {// 有附件
					for (int i=0; i<appendFile.size(); i++) {
						Map<String, String> map = new HashMap<String, String>();
						map = (Map<String, String>)appendFile.get(i);
						String fileName = map.get("fileName").toString();
						String saveName = map.get("saveName").toString();
						// 向appendinfo表插入数据
						sql = "insert into appendinfo values('公告', ?, ?, ?)";
						List<Object> alist = new ArrayList<Object>();
						alist.add(tn);
						alist.add(fileName);
						alist.add(saveName);
						
						OperateDB.setList(alist);
						if (OperateDB.insert(sql)) {// 插入附件表是否成功
							sql = "update publicinfo set isappend = '1' where infoid = "
									+ tn;
							if (OperateDB.update(sql) > 0) {
								isSuccess = true;
							} else {
								isSuccess = false;
							}
						} else {
							isSuccess = false;
						}
					}// for结束
						}
				}

			if (isSuccess) {
				conn.commit();// 手动提交
			} else {
				conn.rollback();
			}
			conn.setAutoCommit(autoCommit);// 恢复现场
		} catch (SQLException e) {
			e.printStackTrace();
			isSuccess = false;
		} finally {
			OperateDB.close(conn);
		}

		return isSuccess;
	}
	
	// 删除appendinfo中的附件
	public static boolean deleteapp(int infoid){
		boolean isSuccess=false;
	// 有附件,则删除附件
		String sql = "delete from appendinfo where append_type = '公告'"
				+ "and fileid = " + infoid;
		//System.out.println(sql);
		int n = OperateDB.update(sql);
		if (n == 0) {// 删除附件失败
			isSuccess = false; 
		}
	 return isSuccess;
	
	}
	
	//删除指定id的公告
	public static boolean deletenote(String[] delID) {
		boolean isSuccess = true;
		if (delID.length < 1) {
			isSuccess = false;
		} else {
			Connection conn = ConnectDB.getConnection();
			boolean autoCommit;
			try {
				autoCommit = conn.getAutoCommit();
				conn.setAutoCommit(false);// 取消自动提交

				for (int i = 0; i < delID.length; i++) {
					int infoid = Integer.parseInt(delID[i]);

					String sql = "select * from publicinfo where infoid = "
							+ infoid;
					System.out.println(sql);
					List<Map> list = new ArrayList<Map>();
					list = OperateDB.executeQuery(sql);
					if (list.size() > 0) {// 存在该项目
						Map map = list.get(0);						
						// 删除appendinfo中的信息
						String isAppend = map.get("isappend").toString();
						if (isAppend.equals("1")) {// 有附件,则删除附件
							sql = "delete from appendinfo where append_type = '公告'"
									+ "and fileid = " + infoid;
							//System.out.println(sql);
							int n = OperateDB.update(sql);
							if (n == 0) {// 删除附件失败
								isSuccess = false;
								break; // 跳出循环
							}
						}

						// 删除publicinfo中的信息
						sql = "delete from publicinfo where infoid = " + infoid;
						//System.out.println(sql);
						int n = OperateDB.update(sql);
						if (n == 0) {// 删除失败
							isSuccess = false;
							break; // 跳出循环
						}

					} else {// 不存在项目
						isSuccess = false;
						break; // 跳出循环
					}
				}

				if (isSuccess != false) {
					conn.commit();// 手动提交
				} else {
					conn.rollback();
				}
				conn.setAutoCommit(autoCommit);// 恢复现场
			} catch (SQLException e) {
				e.printStackTrace();
				isSuccess = false;
			} catch (NumberFormatException e) {
				e.printStackTrace();
				isSuccess = false;
			} finally {
				OperateDB.close(conn);
			}
		}

		return isSuccess;
	}
	
	
	public int getInfoid() {
		return infoid;
	}
	public void setInfoid(int infoid) {
		this.infoid = infoid;
	}
	public String getFromid() {
		return fromid;
	}
	public void setFromid(String fromid) {
		this.fromid = fromid;
	}
	public String getNotetime() {
		return notetime;
	}
	public void setNotetime(String notetime) {
		this.notetime = notetime;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getIsappend() {
		return isappend;
	}
	public void setIsappend(String isappend) {
		this.isappend = isappend;
	}
	
	public int getReadnum() {
		return readnum;
	}

	public void setReadnum(int readnum) {
		this.readnum = readnum;
	}

	public List<Map> getAppendFile() {
		return appendFile;
	}
	
	public String getFromname() {
		return fromname;
	}

	public void setFromname(String fromname) {
		this.fromname = fromname;
	}
}
